package rsj.service.account;

import com.rlyy.aibase.utils.base.Box;
import common.util.Assert;
import org.apache.commons.lang3.StringUtils;
import rsj.api.account.entity.AccountInformation;
import rsj.api.account.entity.AccountQueryParam;
import rsj.api.account.entity.AccountStatus;
import rsj.api.account.entity.AccountTable;
import rsj.dao.UserInfoDao;
import rsj.db.PostgresqlConn;

import java.sql.Connection;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 账号
 *
 * @author yuanqinglong
 * @since 2021/7/14 13:39
 */
public interface IAccountService {


    /**
     * 账户列表
     *
     * @param accountQueryParam 账户查询参数
     * @return {@link Box}
     */
    List<AccountTable> accountList(AccountQueryParam accountQueryParam);


    /**
     * 账户数
     *
     * @param accountQueryParam 账户查询参数
     * @return int
     */
    int accountCount(AccountQueryParam accountQueryParam);


    /**
     * 添加账户
     *
     * @param createAccount 账户
     */
    void addAccount(AccountInformation createAccount);


    /**
     * 更新帐户
     *
     * @param createAccount 创建账户
     */
    void updateAccount(AccountInformation createAccount);


    /**
     * 获得账户
     *
     * @param orgId 机构id
     * @return {@link AccountInformation}
     */
    AccountInformation getAccount(String orgId);


    /**
     * 更新帐户状态
     *
     * @param accountStatus 帐户状态
     */
    void updateAccountStatus(AccountStatus accountStatus);


    /**
     * 验证帐户
     *
     * @param account 账户
     */
    static void verifyAccount(String account) {
        if (StringUtils.isNotEmpty(account)) {
            String regex = "^[0-9a-zA-Z]+$";
            Pattern pattern = Pattern.compile(regex);
            Matcher matcher = pattern.matcher(account);
            Assert.isTrue(matcher.matches(),"账号只能包含英文或数字");

            UserInfoDao userDao = UserInfoDao.getInstance();
            Connection connection = PostgresqlConn.getConnection();
            int count = userDao.verifyUserByUname(connection, account);
            Assert.isTrue(count==0,"账号名已存在");
        }
    }

}
